//
//  SN_ReadRecordViewController.m
//  Shiyun_Newspaper
//
//  Created by Sylar on 2017/6/19.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_ReadRecordViewController.h"
#import "SN_NewspaperInfoViewController.h"

@interface SN_ReadRecordCell : UICollectionViewCell


@property (nonatomic, strong) UIImageView *paperImg;

@end

@implementation SN_ReadRecordCell

- (UIImageView *)paperImg {
    
    if (_paperImg == nil) {
        _paperImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 75, 100)];
        [self.contentView addSubview:_paperImg];
    }
    return _paperImg;
}

@end

@interface SN_ReadRecordViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,reloadDataDelegate>
{
    EmptyStateView *noWifiView;//无网络状态
}

@property (nonatomic, strong) UICollectionView *recordCollection;
@property (nonatomic, strong) NSMutableArray *recordArr;

@end

@implementation SN_ReadRecordViewController

- (void)loadView {
    [super loadView];
    
    self.navBar.navigationItem.title = @"阅读记录";
    
    [self.view setBackgroundColor:colorFromHex(@"ffffff")];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    noWifiView = [[EmptyStateView alloc] initNoWifiWithFrame:CGRectMake(0, (SCREENHEIGH-SafeAreaTopHeight-175)/2+SafeAreaTopHeight, SCREENWIDTH, 175) andBackImage:@"noWifi" andBottomStr:@"网络出错啦,点击重新加载"];
    noWifiView.userInteractionEnabled= YES;
    noWifiView.delegate = self;
    [self.view addSubview:noWifiView];
    
    [self.view addSubview:self.recordCollection];
    [self getRecordData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark -Request

- (void)getRecordData {
    
    NSMutableDictionary *param = [[NSMutableDictionary alloc]init];
    [param setObject:@"api_szb_readhistorylist" forKey:@"method"];
    [param setObject:@"1" forKey:@"pageno"];
    [param setObject:@"20" forKey:@"pagesize"];
    [param setObject:USERID forKey:@"userid"];
    
    [Utils getWithparameters:param showHUD:YES success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary *dataDic = (NSDictionary *)jsonObject;
        
        NSArray *arr = [dataDic objectForKey:@"re_data"];
        
        self.recordArr = [[NSMutableArray alloc]init];
        
        for (NSDictionary *dic in arr) {
            SN_NewspaperModel *model = [SN_NewspaperModel mj_objectWithKeyValues:dic];
            [self.recordArr addObject:model];
        }
        
        self.recordCollection.hidden = NO;
        [self.recordCollection reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        [noWifiView showEmptyImage];
    }];
}

#pragma mark - ReloadData

- (void)reloadCurrentData {
    
    [noWifiView hideEmptyImage];
    [self getRecordData];
    
}

#pragma mark - UICollectionDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.recordArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SN_ReadRecordCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    
    SN_NewspaperModel *paperModel = [self.recordArr objectAtIndex:indexPath.row];
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%ld",IMAGEHOSTURL,paperModel.pic];
    //[cell.paperImg sd_setImageWithURL:[NSURL URLWithString:urlStr]];
    [cell.paperImg sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"newspaper_default"]];
    
    return cell;
}

//每一个分组的上左下右间距
//-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//{
//    return UIEdgeInsetsMake(5, 5, 5, 5);
//}
//
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    return CGSizeMake(75, 100);
//}

#pragma mark - UICollectionDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SN_NewspaperModel *paperModel = [self.recordArr objectAtIndex:indexPath.row];
    
    SN_NewspaperInfoViewController *infoVC = [[SN_NewspaperInfoViewController alloc]init];
    
    infoVC.paperModel = paperModel;
    
    [self.navigationController pushViewController:infoVC animated:YES];
    
}

#pragma mark - Get

- (UICollectionView *)recordCollection {
    
    if (_recordCollection == nil) {
        
        CGFloat itemWidth = 75;
        CGFloat itemHeight = 100;
        
        UICollectionViewFlowLayout *flowLayot = [[UICollectionViewFlowLayout alloc] init];
        flowLayot.sectionInset = UIEdgeInsetsZero;
        flowLayot.itemSize = CGSizeMake(itemWidth, itemHeight);
        flowLayot.minimumLineSpacing = 16;
        
        float space ;
        if (SCREENWIDTH == 320) {
            space = (SCREENWIDTH - 30 - itemWidth*3)/2.0;
        }else {
            space = (SCREENWIDTH - 30 - itemWidth*4)/3.0;
        }
        flowLayot.minimumInteritemSpacing = space;
        
        _recordCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(15, SafeAreaTopHeight+16, SCREENWIDTH-30, SCREENHEIGH- SafeAreaTopHeight - SafeAreaBottomHeight-16) collectionViewLayout:flowLayot];
        _recordCollection.delegate = self;
        _recordCollection.dataSource = self;
        _recordCollection.hidden = YES;
        _recordCollection.showsVerticalScrollIndicator = NO;
        _recordCollection.showsHorizontalScrollIndicator = NO;
        [_recordCollection setBackgroundColor:[UIColor whiteColor]];
        [_recordCollection registerClass:[SN_ReadRecordCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
        
    }
    
    return _recordCollection;
}

@end
